package com.xxx.car.web.api;

import com.xxx.car.entity.vo.request.ReqCouponExchange;
import com.xxx.car.entity.vo.request.ReqCouponQuery;
import com.xxx.car.entity.vo.response.RespCoupon;
import com.xxx.car.service.CouponService;
import com.xxx.common.base.enums.ApiResponseEnum;
import com.xxx.common.base.vo.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by wenjiexu on 2017/6/19.
 */
@Controller
@RequestMapping("/api/coupon")
public class ApiCouponController {

    @Autowired
    private CouponService couponService;

    @RequestMapping("/list")
    @ResponseBody
    public APIResponse list(@Valid @ModelAttribute ReqCouponQuery query, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        couponService.refreshStatus(query.getUserId());

        List<RespCoupon> respCouponList = couponService.query(query);

        return APIResponse.returnSuccess(respCouponList);
    }

    @RequestMapping("/exchange")
    @ResponseBody
    public APIResponse exchange(@Valid @ModelAttribute ReqCouponExchange reqCouponExchange, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        couponService.exchangeCoupon(reqCouponExchange.getUserId(), reqCouponExchange.getCouponTicket());
        return APIResponse.returnSuccess();
    }


}
